// inslude the SPI library:
#include <Flash.h>
#include <SPI.h>
#include <Serial.h>
#include <avr/interrupt.h>
#include <EEPROM.h>

#define AMOUNT 1000

byte testData[16] = {42, 42, 42, 42, 42};
byte zeroData[16] = {0, 0, 0, 0, 0};
byte oneData[16] = {255, 255, 255, 255, 255};

Flash* nandFlash;

void setup() 
{
  Serial.begin(115200);
  nandFlash = new Flash(true, false);
}
/*
unsigned long timeWrite(unsigned long address)
{           
  nandFlash->programExecute(address); 
  unsigned long startTime = micros();
  while (!nandFlash->changeState(STATE_NONE));
  unsigned long endTime = micros();

  return endTime - startTime;
}

unsigned long timeRead(unsigned long address)
{
  nandFlash->pageRead(address);
  unsigned long startTime = micros();
  while (!nandFlash->changeState(STATE_NONE));
  unsigned long endTime = micros();

  return endTime - startTime;
}

void dumpCache(int amount)
{
  nandFlash->readFromCacheStart(0, 0);
  for (int i = 0; i < amount & i < 2112; i++)
  {
    Serial.print(nandFlash->readFromCache());
    Serial.print(" ");
  }
  Serial.println();
  nandFlash->readFromCacheEnd();
}

void zeroCache(int amount)
{
  nandFlash->programLoadStart(0, 0);
  for (int i = 0; i < amount && i < 2112; i++)
  {
    nandFlash->programLoad(0);
  }
  nandFlash->programLoadEnd();
}

void oneCache(int amount)
{
  nandFlash->programLoadStart(0, 0);
  for (int i = 0; i < amount && i < 2112; i++)
  {
    nandFlash->programLoad(255);
  }
  nandFlash->programLoadEnd();
}

void writeCountToCache(int amount)
{
  nandFlash->programLoadStart(0, 0);
  for (int i = 0; i < amount && i < 2112; i++)
  {
    nandFlash->programLoad((byte)i);
  }
  nandFlash->programLoadEnd();
}

void testFlash()
{
  Serial.print("Flash ID: (11314) ");
  Serial.println(nandFlash->readID());

  Serial.println("Zeroed cache");
  zeroCache(AMOUNT);
  dumpCache(AMOUNT);
  Serial.println("Filled cache");
  writeCountToCache(AMOUNT);
  dumpCache(AMOUNT);
  unsigned long writeTime = timeWrite(0x536);
  Serial.print("Wrote to main array in ");
  Serial.print(writeTime);
  Serial.println(" microseconds.");

  Serial.println("Zeroed cache");
  zeroCache(AMOUNT);
  dumpCache(AMOUNT);
  unsigned long readTime = timeRead(0x536);
  dumpCache(AMOUNT);
  Serial.print("Read from main array in ");
  Serial.print(readTime);
  Serial.println(" microseconds.");

  Serial.println("**************************************** DONE **********************************");
}
*/
void loop() 
{  
  //delay(1000);
    
  //testFlash();
  
  // Before you write to any block, make sure you erase it first.
    
  // writebytes will write to the cache. you pass an array of
  // bytes and then the length of that array. Don't write more
  // than 2112 bytes before calling cacheToArray. This moves
  // the current cache into the next available spot in the main
  // array.
  for (int j = 0; j < 10; j++)
  {
    for (int i = 0; i < 30; i++)
    {
      //nandFlash->writeBytes((byte*)&j, 1);
      nandFlash->writeBytes(testData, 5);
    }
    //nandFlash->cacheToArray();    
  }
  Serial.println("done");
  nandFlash->cacheToArray();
  nandFlash->dumpArray(64, 64 + 3, 2112);
      
  while(1);
}


